package com.example.stock.controller;

import com.example.stock.annotation.TokenToUser;
import com.example.stock.business.StockHoldBusiness;
import com.example.stock.business.StockListBusiness;
import com.example.stock.business.StockNameBusiness;
import com.example.stock.entity.User;
import com.example.stock.excel.*;
import com.example.stock.service.*;
import com.example.stock.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@CrossOrigin(origins = "*")
@RequestMapping("stock")
@RestController
public class StockController {

    @Autowired
    StockService stockService;

    @Autowired
    UserService userService;

    @Autowired
    FinanceDataService financeDataService;

    @Autowired
    DailyDataService dailyDataService;

    @GetMapping("info/{stock}")
    public Result info(@PathVariable("stock") String stock ) {
        return ResultGenerator.genSuccessResult(stockService.getStockInfo(stock));
    }

    @GetMapping("finance/{stock}")
    public Result finance(@PathVariable("stock") String stock ) {
        List<Finance> finance = financeDataService.getStockFinanceData().get(stock);
        if (finance == null) {
            return ResultGenerator.genSuccessResult();
        }

        return ResultGenerator.genSuccessResult(
            finance.stream()
                    .filter(item -> item.getSes().length() == 6) // 过滤掉年数据
                    .sorted(Comparator.comparing(Finance::getStatDate))
                    .collect(Collectors.toList())
        );
    }

    @Autowired
    StockHoldBusiness stockHoldBusiness;

    // 持仓个股情况
    @GetMapping("hold")
    public Result hold(@TokenToUser User user) {
        if (!userService.isLogin()) {
            return ResultGenerator.genSuccessResult();
        }

        return ResultGenerator.genSuccessResult(
                stockHoldBusiness.getHoldStocks()
        );
    }

    @GetMapping("tradingRank/{stock}")
    public Result tradingRank(@PathVariable String stock) {
        return ResultGenerator.genSuccessResult(dailyDataService.getDailyData(stock));
    }

    @Autowired
    StockListBusiness stockListBusiness;

    @GetMapping("{type}")
    @Cacheable(value = "StockList", key = "#type")
    public Result index(@PathVariable String type) {
        return ResultGenerator.genSuccessResult(stockListBusiness.getStockList(type));
    }

    @Autowired
    StockNameBusiness stockNameBusiness;

    @GetMapping("roll")
    public Result roll(@RequestParam(required = false) String search) {

        List<String> stocks;
        if (search == null) {
            stocks = stockService.getAllStocksName();
            Collections.shuffle(stocks, new Random());
            return ResultGenerator.genSuccessResult(stocks);
        }

        return ResultGenerator.genSuccessResult(
            stockNameBusiness.getRollStockName(search)
        );
    }
}